我刚开始学习Android
,我遇到了问题。对于新的Android Studio
,我为每个活动都有两个XML
个文件,所以不要得到NullPointerException
我必须稍微更改一下findViewById。但现在我无法更改TextView
中的文字。
代码:
public class MainActivity extends AppCompatActivity {
LinearLayout layoutMain;
TextView tvText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
layoutMain = (LinearLayout) View.inflate(this, R.layout.content_main, null);
tvText = (TextView) layoutMain.findViewById(R.id.tvText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings1:
tvText.setText("one");
Log.d("USER","one");
break;
case R.id.action_settings2:
tvText.setText("two");
Log.d("USER", "two");
break;
}
return super.onOptionsItemSelected(item);
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.myapp6.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
答案 0 :(得分:2)
更改
tvText = (TextView) layoutMain.findViewById(R.id.tvText);
到
tvText = (TextView) findViewById(R.id.tvText);
您收到TextView
的错误引用。
答案 1 :(得分:2)
public class MainActivity extends AppCompatActivity {
View includedView;
TextView tvText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
includedView = (View) findViewById(R.id.includedView);
tvText = (TextView) includedView.findViewById(R.id.tvText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings1:
tvText.setText("one");
Log.d("USER","one");
break;
case R.id.action_settings2:
tvText.setText("two");
Log.d("USER", "two");
break;
}
return super.onOptionsItemSelected(item);
}
}
xml文件中也有一点改变
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.myapp6.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
和第二个xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"
android:id="@+id/includedView"/>
答案 2 :(得分:0)
将content_main
布局复制并粘贴到activity_main
xml中,然后删除content_main
xml文件。
然后将onCreate()
中的代码更改为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
layoutMain = (LinearLayout) findViewById(R.id.content_main);
//content_main should be the id of the layout you copied!
tvText = (TextView) findViewById(R.id.tvText);
}