嗨,所以我对编程很陌生。所以我正在处理的应用程序是一个用数组填充的listView。我添加了一个监听器,这样当从列表中选择一个项目时,它就会打开一个新的活动。现在,新活动的内容基于选择的项目。我可以通过选择列表项来启动新活动。但是当我尝试设置新活动的textview时,应用程序停止运行。我查看了很多相关帖子,但仍无法弄清楚如何设置文字。
继承我的MainActivity.java
package com.example.zach.listview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.zach.listview.R.id.routeDetailsView;
public class MainActivity extends AppCompatActivity {
public static String[] routes = {"Information","Crag Map","Main Wall","1. Shark Bait - 5.9", "2. Rain Check - 5.8", "3. Rain Check Direct - 5.7+",
"4. Arocknophobia - 5.7", "5. Balls Deep - 5.9+", "6. Jingle Bells - 5.9", "7. Itching to Climb - 5.8-", "8. Hog Nose 5.10+",
"9. Slab Dance - 5.10", "10. Don't Screw with My Disco - 5.11c"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
ListAdapter routeAdapter = new CustomAdapter(this, routes);
final ListView routeListView = (ListView) findViewById(R.id.routeListView);
routeListView.setAdapter(routeAdapter);
routeListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String route = String.valueOf(parent.getItemAtPosition(position));
if (route == "1. Shark Bait - 5.9") {
Intent routeDetails = new Intent(view.getContext(), RouteDetails.class);
startActivity(routeDetails);
routeDetailsView.setText("Shark Bait - 5.9");
}
}
}
);
}
}
Heres My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zach.listview.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:id="@+id/routeListView" />
</RelativeLayout>
继承我的RouteDetails.java
package com.example.zach.listview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import static com.example.zach.listview.MainActivity.routes;
public class RouteDetails extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//TextView for route details
final TextView routeDetailsView = (TextView)findViewById(R.id.routeDetailsView);
//if(route == "1. Shark Bait - 5.9"){
//routeDetailsView.setText("Shark Bait - 5.9");
//}
}
}
和我的activity_route_details.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zach.listview.RouteDetails">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="@+id/routeDetailsView" />
</RelativeLayout>
我真的很感激任何帮助!
编辑:这里是我的customAdapter.java
package com.example.zach.listview;
import android.content.Context;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.zach.listview.MainActivity;
/**
* Created by Zach on 11/3/2016.
*/
class CustomAdapter extends ArrayAdapter<String>{
public CustomAdapter(Context context, String[] routes) {
super(context, R.layout.custom_row ,routes);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater routeInflater = LayoutInflater.from(getContext());
View customView = convertView;
if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}
String singleRoute = getItem(position);
TextView routeText = (TextView) customView.findViewById(R.id.routeText);
//for adding count numbers to listView
//int pos = position+1;
//routeText.setText(+pos + ". " + MainActivity.routes [position]);
//pos++;
routeText.setText(singleRoute);
return customView;
}
}
和我的错误消息
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zach.listview, PID: 3137
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.zach.listview.MainActivity$1.onItemClick(MainActivity.java:51)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3126)
at android.widget.AbsListView$3.run(AbsListView.java:4041)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
申请已终止。
答案 0 :(得分:1)
您必须在意向活动之间发送数据:
发送:
Intent routeDetails = new Intent(view.getContext(), RouteDetails.class);
routeDetails.putExtra("value","Shark Bait - 5.9");
startActivity(routeDetails);
并在其他活动中获取数据:
routeDetailsView.setText(getIntent().getExtras().getShort("value"))