////**This is My Fragment Code****////
here i in onclick method based on the option selected in the list specific sql query is executed and gives the output and that output will be display in another activity
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;
}
}
////Activity to get Text from 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////
it is giving null pointer exception for setting the text for 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)
////Activity layout////
<?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>