I'm trying to get the input value from EditText in SearchUser to query in SearchResult
我正在尝试从SearchUser的EditText获取输入值以查询SearchResult。
Here is my Code for SearchUser :
这是我的SearchUser代码:
package com.example.yearbookmuict9sec2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.EditText;
public class SearchUser extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_user);
addButtonListenerHomeButton();
addButtonListenerViewAll();
addButtonListenerSearchButton();
}
private void addButtonListenerSearchButton() {
// TODO Auto-generated method stub
Button button = (Button) findViewById(R.id.SearchButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText edtSearch;
String Answer;
edtSearch = (EditText)findViewById(R.id.answer);
Answer = edtSearch.getText().toString();
Intent goAnswer = new Intent(getApplicationContext(),SearchResult.class);
goAnswer.putExtra("Answer", Answer);
startActivity(goAnswer);
}
});
}
private void addButtonListenerViewAll() {
// TODO Auto-generated method stub
Button button = (Button) findViewById(R.id.viewALLBUTTON);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(SearchUser.this,ViewAll.class);
startActivity(i);
}
});
}
private void addButtonListenerHomeButton() {
// TODO Auto-generated method stub
ImageButton button = (ImageButton) findViewById(R.id.HomeButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(SearchUser.this,Home.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_user, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my activity_search_user :
下面是我的activity_search_user:
<ScrollView
android:id="@+id/scrollView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/home1"
android:layout_alignTop="@+id/scrollView4"
android:background="#fffbf8f0"
android:src="@drawable/ic_home_black_24dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/home1"
android:layout_below="@+id/HomeButton"
android:text="Back Home" />
<Button
android:id="@+id/viewALLBUTTON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/SearchButton"
android:layout_alignBottom="@+id/SearchButton"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/HomeButton"
android:background="#ffff6c41"
android:text="View All"
android:textColor="#ffffffff" />
<ImageView
android:id="@+id/home1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:src="@drawable/logo_180" />
<Button
android:id="@+id/SearchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/home1"
android:layout_below="@+id/home1"
android:layout_marginLeft="56dp"
android:layout_marginTop="47dp"
android:background="#ff606efd"
android:nestedScrollingEnabled="false"
android:text="Search"
android:textColor="#ffffffff" />
<EditText
android:id="@+id/answer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/viewALLBUTTON"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
Here is my code for SearchResult :
这是我的SearchResult代码:
package com.example.yearbookmuict9sec2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class SearchResult extends Activity {
SQLiteDatabase mDb;
Database mHelper;
Cursor mCursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
EditText txtAnswer;
Button btnBack;
String showAnswer;
txtAnswer = (EditText)findViewById(R.id.answer);
btnBack = (Button)findViewById(R.id.SearchButton);
showAnswer = getIntent().getStringExtra("Answer");
txtAnswer.setText(showAnswer);
ListView listView1 = (ListView)findViewById(R.id.listView1);
mHelper = new Database(this);
mDb = mHelper.getWritableDatabase();
mHelper.onUpgrade(mDb, 1, 1);
mCursor = mDb.rawQuery("SELECT " + Database.COL_StudentID + ", "
+ Database.COL_FNAME + ", " + Database.COL_LNAME+ ", "
+ Database.COL_NNAME + " FROM " + Database.TABLE_NAME +" WHERE "
+ Database.COL_StudentID + " = ?", new String[] {showAnswer});
ArrayList<String> dirArray = new ArrayList<String>();
mCursor.moveToFirst();
while ( !mCursor.isAfterLast() ){
dirArray.add("ID : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_StudentID)) + "\n"
+ "Firstname : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_FNAME)) + "\n"
+ "Lastname : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_LNAME)) + "\n"
+ "Nickname : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_NNAME)));
mCursor.moveToNext();
}
ArrayAdapter<String> adapterDir = new ArrayAdapter<String>(getApplicationContext()
, android.R.layout.simple_list_item_1, dirArray);
listView1.setAdapter(adapterDir);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("First Line", "First line of text");
datum.put("Second Line","Second line of text");
data.add(datum);
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2,
new String[] {"First Line", "Second Line" },
new int[] {android.R.id.text1, android.R.id.text2 });
}
public void onPause() {
super.onPause();
mHelper.close();
mDb.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_result, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my activity_search_result :
下面是我的activity_search_result:
<Button
android:id="@+id/backToHome"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/listView1"
android:layout_toEndOf="@+id/imageView"
android:background="#ffafff9b"
android:text="Back to Home" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Student"
android:textColor="#FFFFFF"
android:textSize="40dp" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:cacheColorHint="#00000000" >
</ListView>
</RelativeLayout>
Here is my LogCat :
这是我的LogCat:
12-09 03:57:09.421: E/AndroidRuntime(4961): FATAL EXCEPTION: main
12-09 03:57:09.421: E/AndroidRuntime(4961): Process: com.example.yearbookmuict9sec2, PID: 4961
12-09 03:57:09.421: E/AndroidRuntime(4961): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yearbookmuict9sec2/com.example.yearbookmuict9sec2.SearchResult}: java.lang.NullPointerException
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.os.Looper.loop(Looper.java:136)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-09 03:57:09.421: E/AndroidRuntime(4961): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 03:57:09.421: E/AndroidRuntime(4961): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 03:57:09.421: E/AndroidRuntime(4961): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-09 03:57:09.421: E/AndroidRuntime(4961): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-09 03:57:09.421: E/AndroidRuntime(4961): at dalvik.system.NativeStart.main(Native Method)
12-09 03:57:09.421: E/AndroidRuntime(4961): Caused by: java.lang.NullPointerException
12-09 03:57:09.421: E/AndroidRuntime(4961): at com.example.yearbookmuict9sec2.SearchResult.onCreate(SearchResult.java:39)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.Activity.performCreate(Activity.java:5231)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
12-09 03:57:09.421: E/AndroidRuntime(4961): ... 11 more
Help me please. What i have to do to fix this problem? I'm new here.
请帮助我。我该怎么做才能解决这个问题?我是新来的。
3 个解决方案
#1
0
The problem is sourced on line 39
in SearchResult.java
. You can see this in the LogCat on the line after the one that starts with Caused by: java.lang.NullPointerException
.
问题来自SearchResult.java的第39行。您可以在由:java.lang.NullPointerException开始的行后面的LogCat中看到这一点。
txtAnswer
is not a valid id for an EditText
object in your activity_search_result.xml
, so it returned null
on line 35
when you searched for it. You cannot call .setText()
on a null
object.
txtAnswer不是activity_search_result中的EditText对象的有效id。在第35行搜索时,它返回null。不能在空对象上调用. settext()。
#2
0
txtAnswer is null. This is because it is not in the activity's view. Your activity loads its view as follows:
txtAnswer是null。这是因为它不在活动的视图中。您的活动加载视图如下:
setContentView(R.layout.activity_search_result);
Yet the element with ID answer id not defined in activity_search_result.xml. It is defined in activity_search_user.xml.
但是ID为ID的元素没有在activity_search_result.xml中定义。它在activity_search_user.xml中定义。
So your problem is either:
所以你的问题是:
- a) You are loading the wrong view for your activity OR
- a)您正在为您的活动加载错误的视图
- b) You forgot to add the EditText with ID answer to your view (activity_search_result.xml)
- b)忘记向视图添加ID答案的EditText (activity_search_result.xml)
Edit - looking at your code, I think it is likely that option is is the problem. So to fix this, change
编辑-看你的代码,我认为很可能是选择是问题。为了解决这个问题,改变
setContentView(R.layout.activity_search_result);
to
来
setContentView(R.layout.activity_search_user);
#3
0
You activity_search_result.xml
does not contain any view that you have specified in your activity. You may have to pass right layout xml file in setContentView
or change id
of EditText
and Button
.
你activity_search_result。xml不包含您在活动中指定的任何视图。您可能需要在setContentView中传递正确的布局xml文件,或者更改EditText和按钮的id。
#1
0
The problem is sourced on line 39
in SearchResult.java
. You can see this in the LogCat on the line after the one that starts with Caused by: java.lang.NullPointerException
.
问题来自SearchResult.java的第39行。您可以在由:java.lang.NullPointerException开始的行后面的LogCat中看到这一点。
txtAnswer
is not a valid id for an EditText
object in your activity_search_result.xml
, so it returned null
on line 35
when you searched for it. You cannot call .setText()
on a null
object.
txtAnswer不是activity_search_result中的EditText对象的有效id。在第35行搜索时,它返回null。不能在空对象上调用. settext()。
#2
0
txtAnswer is null. This is because it is not in the activity's view. Your activity loads its view as follows:
txtAnswer是null。这是因为它不在活动的视图中。您的活动加载视图如下:
setContentView(R.layout.activity_search_result);
Yet the element with ID answer id not defined in activity_search_result.xml. It is defined in activity_search_user.xml.
但是ID为ID的元素没有在activity_search_result.xml中定义。它在activity_search_user.xml中定义。
So your problem is either:
所以你的问题是:
- a) You are loading the wrong view for your activity OR
- a)您正在为您的活动加载错误的视图
- b) You forgot to add the EditText with ID answer to your view (activity_search_result.xml)
- b)忘记向视图添加ID答案的EditText (activity_search_result.xml)
Edit - looking at your code, I think it is likely that option is is the problem. So to fix this, change
编辑-看你的代码,我认为很可能是选择是问题。为了解决这个问题,改变
setContentView(R.layout.activity_search_result);
to
来
setContentView(R.layout.activity_search_user);
#3
0
You activity_search_result.xml
does not contain any view that you have specified in your activity. You may have to pass right layout xml file in setContentView
or change id
of EditText
and Button
.
你activity_search_result。xml不包含您在活动中指定的任何视图。您可能需要在setContentView中传递正确的布局xml文件,或者更改EditText和按钮的id。