设置listview适配器时出错
现在让我先告诉你我的程序是什么,我想从数据库中获取数据,因为我已经完成了所有其他的事情,主要关于我的问题是我想从数据库中取出列表。但是一开始我试图用静态内容(android,.net ...)来做它但它给出了一些错误。
这是我的代码
package com.example.z;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import net.sourceforge.jtds.jdbc.*;
public class MainActivity extends Activity {
Button b1,b2,b3,b4,b5;
Button item1,item2,item3;
Button andro,dotnet;
Boolean flag = true;
int i;
ListView list;
TextView t1,t2,t3,t4;
String url = null;
Connection xyz = null;
Connection conn = null;
String s1,s2,s3;
String[] menulist;
ArrayAdapter<String> adapter = null;
String DBName="Lalit";
String UserName="sa";
String Password="123";
Color c;
ResultSet resultSet;
Statement statement = null;
ResultSet rs = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tables);
abc();
Initialise();
}
void abc()
{
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
url ="jdbc:jtds:sqlserver://192.168.1.15;instanceName=MSSQLSERVER;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password;
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
asdf();
}
}, 10000);
Toast.makeText(getBaseContext(), "Connected to server", Toast.LENGTH_SHORT).show();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
void asdf()
{
try {
conn =DriverManager.getConnection(url);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
selectitem();
}
void selectitem()
{
try {
statement=conn.createStatement();
resultSet = statement.executeQuery("SELECT * FROM lalit_db;");
while(resultSet.next()){
s1 = resultSet.getString(1);
s2 = resultSet.getString(2);
s3 = resultSet.getString(3);
Log.w(s2, resultSet.getString(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.w("It is the error", "............");
}
}
void Initialise()
{
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
item1 = (Button)findViewById(R.id.item1);
item2 = (Button)findViewById(R.id.item2);
andro = (Button)findViewById(R.id.android);
dotnet = (Button)findViewById(R.id.dotnet);
t1 = (TextView)findViewById(R.id.menu1);
t2 = (TextView)findViewById(R.id.menu2);
t3 = (TextView)findViewById(R.id.menu3);
t4 = (TextView)findViewById(R.id.menu4);
list=(ListView)findViewById(R.id.listView1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setBackgroundColor(c.TRANSPARENT);
b1.setTextColor(c.RED);
setContentView(R.layout.menu_category);
}
});
String[] values = new String[]{"Adroid Developer",".Net Developer","Java Developer","BA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
// On this next line it gives the error....
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
我有3个xml文件但是这个文件中有列表。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:orientation="vertical" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:marqueeRepeatLimit="5"
android:paddingLeft="50dp"
android:text="Select the Category"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25sp"
android:textStyle="italic" />
<Button
android:id="@+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Developer" />
<Button
android:id="@+id/dotnet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".Net Developer" />
<Button
android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java Developer" />
<Button
android:id="@+id/ba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BA" />
<ListView
android:id="@+id/listView1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
这是tables.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Select the Table"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20sp"
android:textStyle="bold|normal|italic" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_centerHorizontal="true"
android:text="T:3" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button1"
android:text="T:2" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button4"
android:layout_alignParentRight="true"
android:text="T:5" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentLeft="true"
android:text="T:1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="28dp"
android:layout_toLeftOf="@+id/button5"
android:text="T:4" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:text="T:6" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button6"
android:layout_alignBottom="@+id/button6"
android:layout_alignLeft="@+id/button2"
android:text="T:7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_alignLeft="@+id/button3"
android:text="T:8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button8"
android:layout_alignBottom="@+id/button8"
android:layout_alignLeft="@+id/button4"
android:text="T:9" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button9"
android:layout_alignBottom="@+id/button9"
android:layout_alignParentRight="true"
android:text="T:10" />
</RelativeLayout>
这是我的完整Logcat:
01-02 09:15:19.889: E/Trace(5651): error opening trace file: No such file or directory (2)
01-02 09:15:20.729: D/AndroidRuntime(5651): Shutting down VM
01-02 09:15:20.729: W/dalvikvm(5651): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-02 09:15:20.738: E/AndroidRuntime(5651): FATAL EXCEPTION: main
01-02 09:15:20.738: E/AndroidRuntime(5651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.z/com.example.z.MainActivity}: java.lang.NullPointerException
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.os.Looper.loop(Looper.java:137)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-02 09:15:20.738: E/AndroidRuntime(5651): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 09:15:20.738: E/AndroidRuntime(5651): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 09:15:20.738: E/AndroidRuntime(5651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-02 09:15:20.738: E/AndroidRuntime(5651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-02 09:15:20.738: E/AndroidRuntime(5651): at dalvik.system.NativeStart.main(Native Method)
01-02 09:15:20.738: E/AndroidRuntime(5651): Caused by: java.lang.NullPointerException
01-02 09:15:20.738: E/AndroidRuntime(5651): at com.example.z.MainActivity.Initialise(MainActivity.java:169)
01-02 09:15:20.738: E/AndroidRuntime(5651): at com.example.z.MainActivity.onCreate(MainActivity.java:64)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.Activity.performCreate(Activity.java:5104)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-02 09:15:20.738: E/AndroidRuntime(5651): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-02 09:15:20.738: E/AndroidRuntime(5651): ... 11 more
答案 0 :(得分:1)
这是因为您已全局声明
ArrayAdapter<String> adapter = null;
并再次为 ArrayAdapter 创建一个变量
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
因此该变量存在冲突。
因此,对于解决方案Make Global Variable
ArrayAdapter<String> adapter;
并按
初始化adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
编辑:
原因是 NPE ,因为您已经Decalred并初始化该tables.xml文件中的 ListView 这个xml文件中没有它而不是它找到另一个xml文件。你有 空指针异常 。
更新:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setBackgroundColor(c.TRANSPARENT);
b1.setTextColor(c.RED);
LayoutInflater infalter = getLayoutInflater();
View v = infalter.inflate(R.layout.menu_category,null);
list = (ListView)v.findViewById(R.id.listView);
}
});
您可以这样开始新的活动:
Intent n = new Intent(SourceActivity.this , Destination.class);
startActivity(n);
谢谢!
答案 1 :(得分:0)
你的名单是什么? 您可以使用ListView创建布局 并在活动中找到listView,以便您可以setAdapter 这是tutorial
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
活动
ListView listView = (ListView)findViewById(R.id.listView);
// do something with listview
答案 2 :(得分:0)
这是一个基本的tuto:ListView in one class
listView = (ListView) findViewById(R.id.list);
启动适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
调用Listview.setAdapter(adapter)
方法
listView.setAdapter(adapter);
这就是全部:)
修改强>
你做错了。当您致电setContentView(R.layout.tables);
时,您的活动会使该布局膨胀,您可以调用findViewById(ID)
来获取给定布局中的视图。如果视图不在该布局中,则调用findViewById
通常会导致NullPointerException。
否则,如果要使用其他布局的视图,请手动对其进行充气。
要解决您的问题,请移动ListView
中的tables.xml
。
答案 3 :(得分:0)
使用此代码
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import java.util.*;
public class MainActivity extends Activity {
ListView myListView;
EditText addList;
ArrayList<String> todoItems;
ArrayAdapter<String> aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
{
myListView = (ListView)findViewById(R.id.listView1);
addList = (EditText)findViewById(R.id.editText1);
todoItems = new ArrayList<String>();
aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,todoItems);
myListView.setAdapter(aa);
addList.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
todoItems.add(0, addList.getText().toString());
aa.notifyDataSetChanged();
addList.setText("");
return true;
}
return false;
}
});
}
}
}
Xml代码
<?xml version="1.0" encoding="utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
- <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10">
<requestFocus />
</EditText>
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>