App Crash使用actionBar.setTitle();

时间:2015-10-26 09:24:44

标签: android android-actionbar android-sqlite title

我有一个多列表视图,我从Sqlite获取所有数据到每个列表视图

现在我想将第二个列表视图操作栏名称更改为第一个列表视图选择标题名称

当我选择任何一个项目(标题名称)时,将意图传递给下一个活动并设置操作栏但我的应用程序崩溃了 这是代码

我的第一个列表查看代码

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.access$700(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.access$700(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)

4 个答案:

答案 0 :(得分:0)

搜索getActionbar.setTitle("")getSupportActionbar.setTitle("");

答案 1 :(得分:0)

Actionbar actionbar = getActionbar();

or

Actionbar actionbar = getSupportActionBar();

actionbar.setTitle("your title goes here.");

答案 2 :(得分:0)

getActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle Name </font>"));

如果您使用ActionBar的支持库,请将 getActionBar()更改为 getSupportActionBar()

答案 3 :(得分:0)

首先检查你的selectedData字符串和action-bar是否为null,因为你有空指针异常

您可以使用工具栏。并将工具栏设置为操作栏。 在工具栏中添加textview。

    setSupportActionBar(mToolbar);
textView.settext("your list name");

用于参考link