我的问题:应用程序无法启动。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
at info.androidhive.materialtabs.activity.MainActivity.onCreate(MainActivity.java:25)
Main activity line 25 , rellay_timeline.setOnClickListener(new
View.OnClickListener() {
MainActivity.java 类:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
RelativeLayout rellay_timeline;
private Toolbar toolbar;
private Button btnSimpleTabs, btnScrollableTabs, btnIconTextTabs, btnIconTabs, btnCustomIconTextTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rellay_timeline = findViewById(R.id.rellay_timeline999);
rellay_timeline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Activity_Timeline.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btnSimpleTabs = (Button) findViewById(R.id.btnSimpleTabs);
btnScrollableTabs = (Button) findViewById(R.id.btnScrollableTabs);
btnIconTextTabs = (Button) findViewById(R.id.btnIconTextTabs);
btnIconTabs = (Button) findViewById(R.id.btnIconTabs);
btnCustomIconTextTabs = (Button) findViewById(R.id.btnCustomIconTabs);
btnSimpleTabs.setOnClickListener(this);
btnScrollableTabs.setOnClickListener(this);
btnIconTextTabs.setOnClickListener(this);
btnIconTabs.setOnClickListener(this);
btnCustomIconTextTabs.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSimpleTabs:
startActivity(new Intent(MainActivity.this, SimpleTabsActivity.class));
break;
case R.id.btnScrollableTabs:
startActivity(new Intent(MainActivity.this, ScrollableTabsActivity.class));
break;
case R.id.btnIconTextTabs:
startActivity(new Intent(MainActivity.this, IconTextTabsActivity.class));
break;
case R.id.btnIconTabs:
startActivity(new Intent(MainActivity.this, IconTabsActivity.class));
break;
case R.id.btnCustomIconTabs:
startActivity(new Intent(MainActivity.this, CustomViewIconTextTabsActivity.class));
break;
}
}
}
以下是xml
档案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.activity.ScrollableTabsActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="372dp"
android:orientation="horizontal"
android:weightSum="3">
<RelativeLayout
android:id="@+id/rellay_timeline999"
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@color/colorBitterSweet"
android:clickable="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:gravity="center_vertical"
android:src="@drawable/ic_icon_archary"
android:tint="@color/colorWhite" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="@color/colorBitterSweetDark"
android:padding="8dp"
android:text="Stok Transferi"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="14sp" />
</RelativeLayout>
activity_timeline
public class Activity_Timeline extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
答案 0 :(得分:0)
您应该将布局转换为相对布局。我想建议写这样的
rellay_timeline = (RelativeLayout)findViewById(R.id.rellay_timeline999);
答案 1 :(得分:0)
确保它是RelativeLayout组件,或者您找到的ID位于您设置内容视图的同一视图中。
答案 2 :(得分:0)
代码和xml似乎没有任何问题。 您确定,xml是activity_main.xml吗?因为它说
工具:上下文= “info.androidhive.materialtabs.activity.ScrollableTabsActivity”
如果您确定使用的是正确的布局文件,则可以尝试清理并重建项目。有时,生成的R
类会搞砸,然后ID会出错并且可能会发生奇怪的错误。
修改
另一个可能的原因(来自Mohammed Ali发布的链接)是,您在运行应用程序时使用的不同API级别(例如,在layout-v23中)有不同的布局,这与默认布局不同。 / p>