我對android非常陌生。我正在使用Material Design製作滑動標籤佈局,遵循特定的教程。正如我的標題所述,現在我收到了這個錯誤。我從MainActivity調用Main3Activity活動。如果有人能指出錯誤,更重要的是幫助我糾正錯誤。無法啓動活動:java.lang.NullPointerException:
PS。這是我在這裏的第一篇文章,所以提前致歉。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
//getSupprtActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch(id)
{
case R.id.menu_all_reminder:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F44336")));
return true;
case R.id.menu_ToDo:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#03A9F4")));
return true;
case R.id.menu_bills:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4CAF50")));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
這是Main3Activity,這基本上是與滑動佈局的活性。這是一個點擊即可從抽屜式導航的回收站觀的主要活動
Main3Activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationDrawerFragment drawerFragment=(NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mPager);
}
class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment=MyFragment.getInstance(position);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 6;
}
}
public static class MyFragment extends Fragment {
private TextView textView;
public static MyFragment getInstance(int position) {
MyFragment myFragment = new MyFragment();
Bundle args=new Bundle();
args.putInt("position",position);
myFragment.setArguments(args);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout=inflater.inflate(R.layout.fragment_my,container,false);
textView=(TextView)layout.findViewById(R.id.position);
Bundle bundle=getArguments();
if (bundle!=null)
{
textView.setText("The Page Selected is" +bundle.getInt("position"));
}
return layout;
}
}
}
這是明顯的現在
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.demo2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Remind Me"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity android:name=".SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="Demo.Demo2.MainActivity" />
</activity>
<activity android:name=".Main3Activity">
</activity>
</application>
</manifest>
,這是activity_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demo.demo2.Main3Activity"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar"
android:orientation="vertical"
tools:context="demo.demo2.Main3Activity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<tabs.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
app:layout="@layout/fragment_navigation_drawer"
android:layout_gravity="start"
android:name="demo.demo2.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
而logcat中的錯誤
5366-5366/demo.demo2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: demo.demo2, PID: 5366
java.lang.RuntimeException: Unable to start activity ComponentInfo{demo.demo2/demo.demo2.Main3Activity}: java.lang.NullPointerException: Attempt to read from null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to read from null array
at demo.demo2.Main3Activity$MyPagerAdapter.getPageTitle(Main3Activity.java:63)
at tabs.SlidingTabLayout.populateTabStrip(SlidingTabLayout.java:198)
at tabs.SlidingTabLayout.setViewPager(SlidingTabLayout.java:142)
at demo.demo2.Main3Activity.onCreate(Main3Activity.java:45)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-19 02:03:52.050 5366-5366/demo.demo2 I/Process: Sending signal. PID: 5366 SIG: 9
謝謝您提前。
的可能的複製[什麼是空指針異常,怎麼解決呢?(http://stackoverflow.com/questions/218384/what - 它是一個空指針 - 異常和怎麼辦 - 我 - 修復它) –
@ jipr311其實我試着找出錯誤並糾正它。我檢查了鏈接,但找不到解決方案。 –