我对android很新。我正在使用Material Design制作滑动标签布局,遵循特定教程。现在我收到了这个错误,如我的标题所述。我从MainActivity调用Main3Activity活动。如果任何人都可以指出错误,更重要的是帮助我纠正错误。
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,它基本上是滑动布局的活动。这只是点击主要活动
中导航抽屉的Recycler ViewMain3Activity.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
提前谢谢。
答案 0 :(得分:2)
问题是:at demo.demo2.Main3Activity$MyPagerAdapter.getPageTitle(Main3Activity.java:63)
您永远不会初始化或填充tabs
数组,但您尝试在getPageTitle(int position)
方法中使用它:
String[] tabs;
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
我已经在我的strings.xml中使用相同的名称初始化了一个&#34; tabs&#34; 。它不能在这里引用它。
命名与资源同名的字段不会自动加载所述资源。
如果不是,那么我应该在代码中初始化为,String [] tabs = {&#34; Tab1&#34;,&#34; Tab2&#34; &#34; TAB3&#34;}
你可以,但你也可以从资源加载你的字符串数组。您通过Resources
类加载资源。在这种情况下,您可能会使用getStringArray (int id)
方法:
class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}
// ...
}