使用 actionBar.setTitle(); 时应用程序崩溃;
App Crash When using actionBar.setTitle();
我有一个多列表视图,我从 Sqlite 中获取所有数据到每个列表视图
现在我想将第二个列表视图操作栏名称更改为第一个列表视图select离子标题名称
当我 select 任何一个项目(标题名称)使用意图将其传递给下一个 activity 并且我设置了操作栏但我的应用程序崩溃了
这是代码
我的第一个列表视图代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor = dbHelper.gettitles(sqLiteDatabase);
String[] from = new String[]{dbHelper.TITLE};
int[] to = new int[]{R.id.title};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.title_row, cursor, from, to);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor = (Cursor) (listView.getItemAtPosition(position));
String selectedItem = cursor.getString(0);
Intent intent = new Intent(MainActivity.this, SubcategoryActivity.class);
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
我的第二个列表视图代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcategory_activity);
ActionBar actionBar=getActionBar();
listView2= (ListView) findViewById(R.id.listView2);
Intent intent= getIntent();
final String selectedData = intent.getStringExtra("selectedItem");
actionBar.setTitle(selectedData);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase,selectedData);
String[] from = new String[] { dbHelper.SUBCATEGORY };
int[] to = new int[] {R.id.subcategory };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.subcategory_row,cursor,from,to);
adapter.notifyDataSetChanged();
listView2.setAdapter(adapter);
Logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ky.sidd/com.example.ky.sidd.SubcategoryActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access0(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.ky.sidd.SubcategoryActivity.onCreate(SubcategoryActivity.java:60)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access0(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
搜索 getActionbar.setTitle("")
或 getSupportActionbar.setTitle("");
Actionbar actionbar = getActionbar();
or
Actionbar actionbar = getSupportActionBar();
actionbar.setTitle("your title goes here.");
getActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle Name </font>"));
如果您正在使用 ActionBar 的支持库,请将 getActionBar() 更改为 getSupportActionBar()。
首先检查你的 selectedData 字符串和 action-bar 是否为空,因为你有空指针异常
您可以使用工具栏。并将工具栏设置为操作栏。
在工具栏中添加textview。
setSupportActionBar(mToolbar);
textView.settext("your list name");
供参考 link
我有一个多列表视图,我从 Sqlite 中获取所有数据到每个列表视图
现在我想将第二个列表视图操作栏名称更改为第一个列表视图select离子标题名称
当我 select 任何一个项目(标题名称)使用意图将其传递给下一个 activity 并且我设置了操作栏但我的应用程序崩溃了 这是代码
我的第一个列表视图代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor = dbHelper.gettitles(sqLiteDatabase);
String[] from = new String[]{dbHelper.TITLE};
int[] to = new int[]{R.id.title};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.title_row, cursor, from, to);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor = (Cursor) (listView.getItemAtPosition(position));
String selectedItem = cursor.getString(0);
Intent intent = new Intent(MainActivity.this, SubcategoryActivity.class);
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
我的第二个列表视图代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcategory_activity);
ActionBar actionBar=getActionBar();
listView2= (ListView) findViewById(R.id.listView2);
Intent intent= getIntent();
final String selectedData = intent.getStringExtra("selectedItem");
actionBar.setTitle(selectedData);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase,selectedData);
String[] from = new String[] { dbHelper.SUBCATEGORY };
int[] to = new int[] {R.id.subcategory };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.subcategory_row,cursor,from,to);
adapter.notifyDataSetChanged();
listView2.setAdapter(adapter);
Logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ky.sidd/com.example.ky.sidd.SubcategoryActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access0(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.ky.sidd.SubcategoryActivity.onCreate(SubcategoryActivity.java:60)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access0(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
搜索 getActionbar.setTitle("")
或 getSupportActionbar.setTitle("");
Actionbar actionbar = getActionbar();
or
Actionbar actionbar = getSupportActionBar();
actionbar.setTitle("your title goes here.");
getActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle Name </font>"));
如果您正在使用 ActionBar 的支持库,请将 getActionBar() 更改为 getSupportActionBar()。
首先检查你的 selectedData 字符串和 action-bar 是否为空,因为你有空指针异常
您可以使用工具栏。并将工具栏设置为操作栏。 在工具栏中添加textview。
setSupportActionBar(mToolbar);
textView.settext("your list name");
供参考 link