我在主要活动中使用TextView作为链接,当我点击此链接时发生错误 这是我的活动:( 更新 )
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
LayoutInflater inflater = LayoutInflater.from(this);
View layout= inflater.inflate(R.layout.second_activity, null, false);
TextView text = (TextView)layout.findViewById(R.id.mapTextView);
text.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
my second_activity.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/mapTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Test Link"
android:textStyle="bold"
android:textColor="@color/textcolor"
android:textSize="14sp" />
</LinearLayout>
我的适配器类:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.CustomViewHolder> {
...
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
View viewONE = LayoutInflater.from(parent.getContext()).inflate(R.layout.first_activity, parent, false);
CustomViewHolder rowONE = new CustomViewHolder(viewONE);
return rowONE;
case 2:
View viewTWO = LayoutInflater.from(parent.getContext()).inflate(R.layout.second_activity, parent, false);
CustomViewHolder rowTWO = new CustomViewHolder(viewTWO);
return rowTWO;
}
logcat的:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.totoroads.android.app, PID: 5699
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.totoroads.android.app/com.totoroads.android.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.totoroads.android.app.MainActivity .onCreate(MainActivity.java:100)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
varialbe mapTextView 我没有在main_activity.xml中设置并在其他(second_activity.xml)中设置
如何在其他布局中调用mapTextView?谢谢!
答案 0 :(得分:0)
当你没有设置它存储null因此你需要初始化它
先试试mapTextView = findViewById(R.id.mapTextView);
主要活动示例
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Now second activity you want to switch
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
}
答案 1 :(得分:0)
你无法调用屏幕上不存在的视图!!
但是,如果您的SecondActivity正在运行。您可以使用界面或EventBus库连接到TextView。
答案 2 :(得分:0)
setContentView(R.layout.activity_main);
正如您在MainActivity中所做的那样,现在您只能参考activity_main中的所有对象。
现在为了实现您想要的功能,(获取 second_activity.xml 中的textview参考),您必须使用布局充气器来扩充second_activity.xml然后,您可以在MainActivity中执行和处理 OnClick 任务。
LayoutInflater inflater = LayoutInflater.from(this);
View layout= inflater.inflate(R.layout.second_activity.xml, null, false);
TextView text = (TextView)layout.findViewById(R.id.mapTextView);
text.setOnClickListener(this);
您还可以在 activity_main.xml 中加入 second_activity.xml ,以实现您的功能。