我在使用recyclerView时遇到了问题 . 这是xml中的定义:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/tlbMenuDetalle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rcyViatgesDetall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
我尝试在以下类中使用它:
public class ViatgeDetall extends AppCompatActivity {
private RecyclerView mRcyViatgesDetall;
private AdapterViatgesDetall mAdapterViatgesDetall;
private ViatgesManager.Viatge mViatge;
private ArrayList<ViatgesManager.Viatge> viatges;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.tlbMenuDetalle);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("TravelApp");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Log.d("myTag", "Detalle OnCreate" );
mRcyViatgesDetall = (RecyclerView) findViewById(R.id.rcyViatgesDetall);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
viatges = ViatgesManager.ITEMS;
int f = getIntent().getIntExtra("viatge",0);
mViatge = viatges.get(f-1);
Log.d("myTag", "Detalle OnCreate" + mViatge.getName());
}
ArrayList<ViatgesManager.Stop> stops = mViatge.getStops();
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
GridLayoutManager glm = new GridLayoutManager(this,2);
glm.setOrientation(GridLayoutManager.HORIZONTAL);
mRcyViatgesDetall.setLayoutManager(llm);
mAdapterViatgesDetall = new AdapterViatgesDetall(stops, this);
mRcyViatgesDetall.setAdapter(mAdapterViatgesDetall);
}
}
我不知道我在做什么,因为我不断收到这个错误:
FATAL EXCEPTION:main进程:com.example.usuari.myapplication3,PID:26507 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.usuari.myapplication3 / com.example.usuari.myapplication3.ViatgeDetall}:java .lang.NullPointerException:尝试在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)上的空对象引用上调用虚方法'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' )android.app.A活动时,Android.app.A活动(活动传递 . 密码:)在Android.app.ActivityThread.access $ 1100(ActivityThread.java:222)的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1795)处于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) android.os.Handler.dispatchMessage(Handler.java:102)位于android.app.Looper.loop(Looper.java:158),位于java.lang.reflect的android.app.ActivityThread.main(ActivityThread.java:7229) com.android.int上的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)中的.Method.invoke(Native Method) ernal.os.ZygoteInit.main(ZygoteInit.java:1120)引起:java.lang.NullPointerException:尝试调用虚方法'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)'on位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)的android.app.Activity.performCreate(Activity.java:6876)的com.example.usuari.myapplication3.ViatgeDetall.onCreate(ViatgeDetall.java:37)中的空对象引用:1135)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)at android.app.ActivityThread.access $ 1100(ActivityThread.java:222)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1795)在android.app.Handler.dispatchMessage(Handler.java:102)android.app.Looper.loop(Looper.java:158)android.app .ActivityThread.main(ActivityThread.java:7229)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCall er.run(ZygoteInit.java:1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
即使我评论关于工具栏的部分我得到一个非常类似的错误,但与RecyclerView . 我正在使用sdk 25,如果这有帮助...
这是我的表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usuari.myapplication3">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EditViatge"
android:label="EditVIatge"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ViatgeDetall"
android:label="ViatgeDetall"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2 回答
您必须添加正确的布局文件我认为您放置了错误的布局文件名
错误发生在:
这不是正确的布局设置 . 相反它应该是prevoiusly显示的xml的布局...