0
當用戶點擊按鈕時,我試圖從片段中啓動活動時該應用程序強制退出(按鈕位於片段中,並且應該啓動活動)。當從片段中啓動活動時發生Java運行時異常
我試圖從片段中推出一個活動時,遇到下列錯誤(顯示的相關logcat的輸出):
...
03-13 18:07:27.023: E/AndroidRuntime(12925): FATAL EXCEPTION: main
03-13 18:07:27.023: E/AndroidRuntime(12925): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.vutoch.myapp/com.vutoch.myapp.Activities.TextViewActivity}: java.lang.NullPointerException
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.app.ActivityThread.access$600(ActivityThread.java:153)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.os.Looper.loop(Looper.java:137)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.app.ActivityThread.main(ActivityThread.java:5227)
03-13 18:07:27.023: E/AndroidRuntime(12925): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 18:07:27.023: E/AndroidRuntime(12925): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 18:07:27.023: E/AndroidRuntime(12925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-13 18:07:27.023: E/AndroidRuntime(12925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-13 18:07:27.023: E/AndroidRuntime(12925): at dalvik.system.NativeStart.main(Native Method)
03-13 18:07:27.023: E/AndroidRuntime(12925): Caused by: java.lang.NullPointerException
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
03-13 18:07:27.023: E/AndroidRuntime(12925): at com.vutoch.myapp.Activities.TextViewActivity.<init>(TextViewActivity.java:27)
03-13 18:07:27.023: E/AndroidRuntime(12925): at java.lang.Class.newInstanceImpl(Native Method)
03-13 18:07:27.023: E/AndroidRuntime(12925): at java.lang.Class.newInstance(Class.java:1319)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-13 18:07:27.023: E/AndroidRuntime(12925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
03-13 18:07:27.023: E/AndroidRuntime(12925): ... 11 more
從我TextViewFragment中,我試圖啓動TextViewActivity。這給出了上述錯誤。
TextViewFragment:
package com.vutoch.myapp.Fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.vutoch.myapp.R;
import com.vutoch.myapp.Activities.SettingsActivity;
import com.vutoch.myapp.Activities.TextViewActivity;
import com.vutoch.myapp.Utils.DownloadFile;
import com.vutoch.myapp.Utils.GeneralUtils;
public class TextViewFragment extends Fragment {
private static View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_test, container, false);
// Download the file and return its path.
String newFilePath = new DownloadFile().createDownloadFile();
Log.v("FRAG_FILEPATH", "Path = " + newFilePath);
// If the button is clicked, the app force quits!
final Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newIntent = new Intent(view.getContext(), TextViewActivity.class);
//Intent newIntent = new Intent(TextViewFa, TextViewActivity.class);
startActivity(newIntent);
}
});
//Intent newIntent = new Intent(this.getActivity(), TextViewActivity.class);
//startActivity(newIntent);
}
catch (InflateException e) {
final String errorMsg = "The text view could not be loaded.";
GeneralUtils.showDialogBox(view.getContext(), "Text View Fragment Error", errorMsg);
Log.v("TextView_Inflate_Fail", errorMsg);
}
return view;
}
}
是TextviewFragment嘗試啓動該活動如下:
TextViewActivity:
package com.vutoch.myapp.Activities;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.vutoch.myapp.R;
import com.vutoch.myapp.Database.DbHelper;
import com.vutoch.myapp.Database.DbQuery;
@SuppressLint("SdCardPath")
public class TextViewActivity extends Activity {
ListView listView;
final String DB_FULL_PATH = "/data/data/" + this.getPackageName() + "/databases/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_textview);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
DbHelper dbh = new DbHelper(this);
try {
dbh.createDatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Create database failed.");
e.printStackTrace();
}
SQLiteDatabase db = dbh.getReadableDatabase();
// Get all entries from the MTOresults table.
// We use this as a sample for now.
String[] whereArgs = { "1" };
Cursor values = DbQuery.runSqlQuery(db, "SELECT * FROM MTOresults where Iid >= ?", whereArgs);
/* Start list adapter code -- DO NOT REMOVE */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position:" +itemPosition
+" ListItem: " + itemValue , Toast.LENGTH_LONG).show();
}
});
/* End list adapter code */
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
供參考,這是我的Android清單文件:
AndroidManifest.XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vutoch.myapp"
android:versionCode="1"
android:versionName="2.0-build20140209-tony" >
<permission
android:name="com.vutoch.myapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.vutoch.myapp.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Use openGL -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCd2BdcE8rInSxBZ9QXdPdYviOIvTtMpUo" />
<activity
android:name="com.vutoch.myapp.Activities.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.vutoch.myapp.Activities.SettingsActivity"
android:label="@string/title_activity_settings" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.vutoch.myapp.Activities.MainActivity" />
</activity>
<activity
android:name="com.vutoch.myapp.Activities.TextViewActivity"
android:label="Text View" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.vutoch.myapp.Activities.MainActivity" />
</activity>
</application>
我希望這是所有必要的信息(我是Android的新手)。我認爲,這是導致問題的行是啓動明確意圖(在TextViewFragment)的一個:至於爲什麼它不工作
Intent newIntent = new Intent(view.getContext(), TextViewActivity.class);
任何想法?無法弄清楚。謝謝!
這是真的太多的代碼。花一些時間來減少它,你會得到更多的幫助。就目前來看,這是太多的工作。 – larsAnders