Layout->
activity_main.xml
content_main.xml
Logic ->
ActivityMain.java
MainLogic.java
2 Issues: (second issue is the actual issue)
1) java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.app./com.test.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object referenc
2) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2124) at com.test.app.MainLogic.CreateListView(MainLogic.java:51)
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" 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:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main"
android:id="@+id/main_layout">
<LinearLayout
android:id="@+id/linLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/origin_string"
android:textSize="20sp"/>
<TextView
android:id="@+id/textView2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/random_string_sample"
android:textSize="20sp"
/>
</LinearLayout>
<Button
android:id="@+id/btnClick1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:text="@string/app_name" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview1"
android:layout_below="@+id/btnClick1">
</ListView>
</RelativeLayout>
ActivityMain.java->
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.logo);
start();
}
void start(){
ListView listView = (ListView) findViewById(R.id.listview1); //ADDED
MainLogic ml = new MainLogic();
ml.CreateListView(>ADDED: listview<);
}
}
MainLogic.java->
EDIT: added class line below Removed extends Activity
public class MainLogic {
public void CreateListView() {
final ListView list_View1 = (ListView) findViewById(R.id.listview1);
final String[] listViewStrings = new String[]{"first_item", "second_item", "third_item", "fouth_item", "fifth_item", "sixth_item", "seventh_item", "eigth_item"};
ArrayAdapter list_adapter = new ArrayAdapter<String>(`<s>`getApplicationContext()`</s>`, android.R.layout.simple_list_item_1,listViewStrings);
//list_adapter.addAll(>REMOVED: listViewStrings.toString()<);
list_View1.setAdapter(list_adapter); // Use data, pass the adapter with the strings, numbers, etc.
}
}
Whom ever does answer this if you can explain why I am doing things wrong and also possibly show me how to fix the code, that would be great. I have read over these links:
http://www.vogella.com/tutorials/AndroidListView/article.html
http://developer.android.com/guide/topics/ui/layout/listview.html
http://www.tutorialspoint.com/android/android_list_view.htm
I do not see the inherent flaw in my code except that i am implementing the logic in an external class then creating an object to call the method.
SOLUTION ->
ActivityMain.java->
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.logo);
start();
}
public void start(){
MainLogic ml = new MainLogic();
ml.CreateListView(this);
}
}
MainLogic.java->
public class MainLogic {
public void CreateListView(final MainActivity newActivity) {
final ListView list_View1 = (ListView) newActivity.findViewById(R.id.listview1);
final String[] listViewStrings = new String[]{"first_item", "second_item", "third_item", "fouth_item", "fifth_item", "sixth_item", "seventh_item", "eigth_item"};
// This will adapt array that listview can work with i.e. generate all rows
ArrayAdapter list_adapter = new ArrayAdapter<>(newActivity.getApplicationContext(), android.R.layout.simple_list_item_1, listViewStrings);
list_adapter.addAll();
list_View1.setAdapter(list_adapter); // Use data, pass the adapter with the strings, numbers, etc.
list_View1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position_in_array, long id) {
// Logic for what happens when clicked
// Context => http://stackoverflow.com/questions/3572463/what-is-context-in-android
int position = position_in_array;
String item_value = (String) adapter.getItemAtPosition(position);
String item = "Item: " + listViewStrings.toString() + " / value " + item_value + " ." ;
Toast.makeText(newActivity.getApplicationContext(), item, Toast.LENGTH_SHORT).show();
}
});
} }
public void CreateListView() { ... **list_adapter.addAll(); // CHANGED THIS LINE** ... }
Can anyone tell me why this is not able to be run outside the MainActivity.java file, is it the context, is it the permissions, is it the reference to **final ListView list_View1 = (ListView) findViewById(R.id.listview1);** or something else? – codelinx Dec 07 '15 at 03:45