我的闪屏有第一个活动,第二个活动从我的导航可绘制的activity扩展而来。所以问题是,当我转到第二个活动,并转到处理导航位置的函数时,我得到了空指针错误,我不知道为什么会这样。
这是我的第一个活动代码:
package com.example.uniapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SplashScreen extends Activity {
Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.button);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SplashScreen.this,Description.class);
startActivity(intent);
finish();
}
});
}
///////////////////////////////////////////////////////////////some function this functions is not use
public int testdb()
{
int count = 0;
SQLiteDatabase mydb;
try {
Toast.makeText(SplashScreen.this, "asdasddasds", Toast.LENGTH_LONG).show();
MyDatabase MyDataBase = new MyDatabase(this);
mydb = MyDataBase.getReadableDatabase();
Cursor cursor = mydb.rawQuery("SELECT * FROM news",null);
cursor.moveToFirst();
cursor.moveToNext();
String TempTest = cursor.getString(cursor.getColumnIndex("title"));
if(!TempTest.equals(""))
count = 5;
if(cursor != null)
{
//cursor.moveToFirst();
count = cursor.getInt(0);
Toast.makeText(getApplicationContext(), "null!!!!!!!!!!" , Toast.LENGTH_LONG).show();
}
cursor.close();
mydb.close();
Toast.makeText(getApplicationContext(), "database 11 done : " + count , Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO: exception daa base
Toast.makeText(getApplicationContext(), "data error 1 : " + 0, Toast.LENGTH_LONG).show();
}
return count;
}}
这是我的第二个活动代码:
package com.example.uniapp;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Description extends DrawableNav implements OnItemClickListener {
int drawableposition;
MyArray myarray;
MyDatabase MyDataBase;
SQLiteDatabase mydb;
int count = 0;
ListView MyList;
TextView showInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//////////////////////////////////////////////////////////////////////////////////////////////////*inflaste layout
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate your activity layout here!
View contentView = inflater.inflate(R.layout.newdescription, null, false);
mDrawerLayout.addView(contentView, 0);
//////////////////////////////intilitize list view
MyList = (ListView)findViewById(R.id.list);
showInfo = (TextView) findViewById(R.id.show_uniInfo);
//////////////////////////////get intent
//Intent intent = getIntent();
//count = intent.getIntExtra("db", 0);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<cases
public void SelectItem(int possition) {
drawableposition = possition;
if(possition != 0)
showInfo.setVisibility(View.GONE);
switch (possition) {
case 0 :
System.out.println("<<<<<<<<<<<<<<<<<<<test for exception");
showInfo.setVisibility(View.VISIBLE); ///get null pointer here by all codes
break;
case 1:
////////////// codes work well
break;
}
}
}我更困惑的是,一些吐司工作there.when,我在那里,它没有问题,但我不能使文本视图可见。
09-13 22:16:48.708: E/AndroidRuntime(28067):致命异常: main 09-13 22:16:48.708: E/AndroidRuntime(28067):进程: com.example.uniapp,ComponentInfo{com.example.uniapp/com.example.uniapp.Description}::28067 09-13 22:16:48.708: E/AndroidRuntime(28067):android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404):Unable to start activity AndroidRuntime 09-13 22:16:48.708: E/AndroidRuntime(28067):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java 09-13 22:16:48.708: E/AndroidRuntime(28067):at activity09-13 22:16:48.708: E/AndroidRuntime(28067):at android.app.ActivityThread.access$900(ActivityThread.java:172) 09-13 22:16:48.708: E/AndroidRuntime(28067):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 09-13 22:16:48.708: E/AndroidRuntime(28067):at android.os.Handler.dispatchMessage(Handler.java:102) 09-13 22:16:48.708: E/AndroidRuntime(28067):在android.os.Looper.loop(Looper.java:146) 09-13 22:16:48.708: E/AndroidRuntime(28067):在android.app.ActivityThread.main(ActivityThread.java:5653) 09-13 22:16:48.708: E/AndroidRuntime(28067):在java.lang.reflect.Method.invokeNative(原生方法) 09-13 22:16:48.708: E/AndroidRuntime(28067):在java.lang.reflect.Methodcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) (.invoke) 09-13 22:16:48.708: E/AndroidRuntime(28067):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 09-13 22:16:48.708: E/AndroidRuntime(28067):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 09-13 22:16:48.708: E/AndroidRuntime(28067):at dalvik.system.NativeStart.main(Native Method) 09-13 22:16:48.708: E/AndroidRuntime(28067):原因: com.example.uniapp.Description.SelectItem(Description.java:94) 09-13 22:16:48.708: E/AndroidRuntime(28067):at com.example.uniapp.DrawableNav.onCreate(DrawableNav.java:109) 09-13 22:16:48.708: E/AndroidRuntime(28067):at java.lang.NullPointerException 09-13 22:16:48.708: E/AndroidRuntime(28067):atcom.example.uniapp.Description.onCreate(Description.java:61) 09-13 22:16:48.708: E/AndroidRuntime(28067):at android.app.Activity.performCreate(Activity.java:5541) 09-13 22:16:48.708: E/AndroidRuntime(28067):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 09-13 22:16:48.708: E/AndroidRuntime(28067):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368) 09-13 22:16:48.708: E/AndroidRuntime(28067):... 11
我还发现,如果我从我的导航可绘制活动中删除了这段代码,一切都会正常工作
代码如下:
if (savedInstanceState == null) {
SelectItem(0);
}此代码在活动首次出现时调用第一个位置的操作
发布于 2015-09-14 01:15:45
我的客人是,您没有在onCreate方法中初始化您的mDrawerLayout,可能是从onCreate方法中的下面一行引发的异常...!
mDrawerLayout.addView(contentView, 0);请先初始化mDrawerLayout,如下所示:
mDrawerLayout = (DrawerLayout) findViewById (R.id.your_drawer_layout_id);还有一件事,你没有像下面这样在活动中设置内容,
setContentView(int);发布于 2015-09-14 02:35:19
初始化mDrawerLayout,请尝试:
showInfo = (TextView) contentView.findViewById(R.id.show_uniInfo);Insted of:
showInfo = (TextView) findViewById(R.id.show_uniInfo);https://stackoverflow.com/questions/32552273
复制相似问题