//// **这是我的片段代码**** ////
这里我在onclick方法基于列表中选择的选项执行特定的sql查询并给出输出,该输出将显示在另一个活动中
public class TouristPlace extends Fragment implements AdapterView.OnItemClickListener {
public ListView list;
public ArrayAdapter<String> arrayAdapter;
public ResultSet result;
public Statement statement;
public String Desc;
public String output;
public TouristPlace touristPlace;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tourists, container, false);
list = (ListView)rootView.findViewById(R.id.places);
touristPlace=new TouristPlace();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Welcome_to_hyd", "root", "test");
statement = con.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
final String[] placelist = getResources().getStringArray(R.array.menu);
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, placelist);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(this);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
output=touristPlace.result("Golconda");
Intent i = new Intent(getActivity(), ListNavigation.class);
i.putExtra("DatabaseOutput", output);
startActivity(i);
}
}
public String result(String city){
String Fort = "Select Description from testHyd where TouristPlace="+city;
try {
result = statement.executeQuery(Fort);
while (result.next()) {
Desc=result.getString(1);
System.out.println(Desc);
}
}catch (Exception e){
e.printStackTrace();;
}
return Desc;
}
}
//// 从Fragment获取文本的活动 ////
public class ListNavigation extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.placesoutput);
TextView tv=findViewById(R.id.hydtext1);
tv.setText(getIntent().getStringExtra("DatabaseOutput"));
}
}
////的 logcat的 ////
它为textview
设置文本提供了空指针异常Process: com.cityzers.welcometohyderabad, PID: 2973
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cityzers.welcometohyderabad/com.cityzers.welcometohyderabad.ListNavigation}: 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:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
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.cityzers.welcometohyderabad.ListNavigation.onCreate(ListNavigation.java:23)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
////活动布局////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<ImageView
android:id="@+id/imageView2"
android:layout_width="357dp"
android:layout_height="150dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="13dp"
android:src="@drawable/golconda"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="16dp"
android:textStyle="bold"
android:layout_below="@id/imageView2"
android:layout_marginTop="15dp"
android:id="@+id/textview"/>
</RelativeLayout>
答案 0 :(得分:0)
ID不匹配的错误,在您的布局中,您提供了ID android:id="@+id/textview"
,并且在您的活动中,您正在执行TextView tv=findViewById(R.id.hydtext1);
两个ID都不同。在布局XML中没有textView
id为hydtext1。
答案 1 :(得分:0)
在你的onCreate方法中,你试图找到你的XML布局中不存在的id“hydtext1”。
TextView tv=findViewById(R.id.hydtext1);
因此setText函数正在标记NPE。将其更改为
TextView tv=findViewById(R.id.textview);
答案 2 :(得分:0)
这是因为在您的placesoutput
版面中,您没有任何带有hydtext1
ID的TextView。您的布局应该包含TextView,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
...
<TextView
android:id="@+id/hydtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="16dp"
android:textStyle="bold"
android:layout_below="@id/imageView2"
android:layout_marginTop="15dp"/>
</RelativeLayout>
这是一个常见问题,因为没有使用命名约定。为了防止将来发生同样的问题,您需要使用适当的命名约定。
以下是惯例:
活动名称
您应该使用Activity
作为其后缀来命名您的活动。例如,使用ListNavigationActivity
代替ListNavigation
。
布局名称
使用布局的活动名称但按向后顺序。例如,使用activity_list_navigation.xml
代替placesoutput.xml
。
查看名称。
您应该使用布局名称在布局中命名视图,并将视图的缩写添加为后缀。例如,如果activity_list_navigation.xml
内的TextView需要将其命名为list_navigation_name_tv