java - 通过意图在选项卡式活动中编辑 TextView - Android
问题描述
我有一个活动,它从用户那里获取两个变量(重量和执行的次数),然后我进行一些计算并通过这个意图发送最终结果:
Intent intent = new Intent(this,calculated_2.class);
intent.putExtra("EXTRA_MESSAGE","your calculated one rep max is "+onerepmax+ " KG");
startActivity(intent);
在我的第一次尝试中,我使用 a 进行了活动,并使用以下代码对其进行了Textview
编辑:Textview
Intent intent = getIntent();
String message = intent.getStringExtra("EXTRA_MESSAGE");
TextView tv = findViewById(R.id.one);
tv.setText(message);
但是,我将该活动切换到选项卡式视图。
这是它的代码:
public class calculated_2 extends AppCompatActivity {
private static final String TAG = "Calculate";
private SectionsPageAdapter spa;
private ViewPager mviewPager;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculated_2);
Log.d(TAG,"onCreate: Starting. ");
Intent intent = getIntent();
String message = intent.getStringExtra("EXTRA_MESSAGE");
TextView tv = findViewById(R.id.one);
tv.setText(message);
spa = new SectionsPageAdapter(getSupportFragmentManager());
mviewPager =(ViewPager) findViewById(R.id.container);
setViewPager(mviewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mviewPager);
}
private void setViewPager(ViewPager viewPager){
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.AddFragment(new tab1_fragment(),"One Rep Max");
adapter.AddFragment(new tab2_fragment(),"5-3-1");
viewPager.setAdapter(adapter);
}
}
这是现在包含该文本视图的第一个选项卡的代码:
public class tab1_fragment extends Fragment {
private static final String TAG = "oneRepMax";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.tab1_fragment,container,false);
return view;
}
}
然而,with this code im not able to edit the textview and my app crashes.
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.khedir.ufp, PID: 19412
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.khedir.ufp/com.khedir.ufp.calculated_2}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.khedir.ufp.calculated_2.onCreate(calculated_2.java:45)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".calculated_2">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
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:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
包含 TextView 的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".calculated_2">
<TextView
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Your Calculated 1RM"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
解决方案
正如日志建议您在对象引用上使用setText方法。null
要解决这个问题,请确保您已包含TextView
在id
布局文件中。
这TextView tv = findViewById(R.id.one);
确保有带有one
id 的 TextView。
推荐阅读
- node.js - 每次我尝试将我的反应代码推送到 heroku 时,git push heroku main 都会失败
- opencv - 跟踪记住原始 ID 的对象
- .net - 在 .Net Core 中打开工作流 xaml 文件时出错
- python - 如何将与其计数器列值计数的列值外推到新的数据帧?
- python - Displot 整数之间的下降
- c - 如何将结构数组放置在 RAM 中的特定位置,例如 STM32F4 MCU 中的备份 RAM?
- apache-kafka - Prometheus - 计算百分比增加
- algorithm - 无法理解为什么这不是高度平衡的二叉搜索树
- android - android studio ( kotlin ) build.gradle 错误?
- c# - 通过坐标查找字符串的索引