2012-05-27 3 views
2

Хорошо, поэтому я новичок в программировании, и это мой первый проект программирования. Я хотел бы создать приложение «Мотивационные котировки», в котором он состоял бы из 10 человек с несколькими кавычками. Просто какой-то простой просмотр-выборка и рандомизация массива. Я был в процессе кодирования части с отображением просмотров, а затем, когда я попытался запустить ее в эмуляторе, это дает мне «приложение неожиданно остановилось». Logcat показывает «FATAL EXCEPTION Main». Надеюсь, кто-то может помочь мне в этом, я застрял в тупике, и закрытие даты истекает быстро.Android: Fatal Exception Main

Я попытался искать вокруг, и большинство просит Src, main.xml и Manifest.xml поэтому я просто разместить их здесь:

ЦСИ:

package first.motivationalquotes; 

import java.util.Random; 
import android.app.Activity; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.ViewFlipper; 

public class MotivationalQuotesActivity extends Activity { 
    /** Called when the activity is first created. */ 

Button next,previous; 
ViewFlipper vf; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     next = (Button) findViewById(R.id.button1); 
     previous = (Button) findViewById(R.id.button2); 
     next.setOnClickListener((OnClickListener) this); 
     previous.setOnClickListener((OnClickListener) this); 
     vf=(ViewFlipper)findViewById(R.id.viewFlipper1); 
    } 

    public void randomize(View v) 
    { 
     Random ran = new Random(); 
     String[] ButtonText = null; 
     Resources res = getResources(); 
     ButtonText = res.getStringArray(R.array.Quotes); 
     String strRandom = ButtonText[ran.nextInt(ButtonText.length)]; 
     System.out.println("Random string is : "+strRandom); 

     TextView tv = (TextView) findViewById(R.id.textView2); 
     tv.setText(ButtonText[ran.nextInt(ButtonText.length)]); 
    } 

    public void onClick(View v) { 
     if (v == next) { 
      vf.showNext(); 
     } 
     if (v == previous) { 
      vf.showPrevious(); 
     } 
    } 

} 

Следующая является main.xml (только кодируется для 2-х человек, как сейчас):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ViewFlipper 
     android:id="@+id/viewFlipper1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <!-- View 1- Einstein - person1 --> 
     <TableLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TableRow> 
     <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Albert Einstein" 
     android:layout_gravity="center" 
     android:textSize="28dp" > 
     </TextView> 
     </TableRow> 

     <TableRow> 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:src="@drawable/clear_einstein" /> 

     </TableRow> 

     <TableRow> 
     <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/einstein1" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

    </TableLayout> 

     <!-- View 2- Tesla - person2 --> 
    <TableLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TableRow> 
     <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Nikola Tesla" 
     android:layout_gravity="center" 
     android:textSize="28dp" > 
     </TextView> 
     </TableRow> 

     <TableRow> 
     <ImageView 
     android:id="@+id/imageView1" 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/nikola_tesla" /> 
     </TableRow> 

     <TableRow> 
     <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/einstein1" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

    </TableLayout> 

    <TableLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TableRow> 
     <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Albert Einstein" 
     android:layout_gravity="center" 
     android:textSize="28dp" > 
     </TextView> 
     </TableRow> 

     <TableRow> 
     <ImageView 
     android:id="@+id/imageView1" 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/clear_einstein" /> 
     </TableRow> 

     <TableRow> 
     <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/einstein1" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

    </TableLayout> 

    </ViewFlipper> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_gravity="center"> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="button2Click" 
     android:clickable="true" 
     android:text="&lt;Previous" 
     android:layout_gravity="center" /> 
    <Button 
     android:id="@+id/aboutMeButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="randomize" 
     android:text="Randomize" 
     android:layout_gravity="center"/> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="button1Click" 
     android:text="Next&gt;" 
     android:layout_gravity="center"/> 
    </LinearLayout> 

    </LinearLayout> 

тогда manifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="first.motivationalquotes" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".MotivationalQuotesActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

Logcat: http://i56.photobucket.com/albums/g183/luq_man/Logcat.png

+1

Пожалуйста, напишите Ваше содержание LogCat, а также. –

ответ

1

Я думаю, вы должны реализовать OnClickListener.

общественного MotivationalQuotesActivity продлить активность реализует OnClickListener {

...... }

+0

Хорошо, спасибо, не могу поверить, как легко было его решить. – user1420185

+0

Извините, что опоздал, но программа снова рухнула, когда вы нажимаете «Next» или «предыдущий» после перехода в другое представление. – user1420185

+0

05-31 04: 35: 32.013: E/AndroidRuntime (424): java.lang.IllegalStateException: Не удалось найти метод button1Click (View) в классе активности first.motivationalquotes.MotivationalQuotesActivity для обработчика onClick в классе вида android.widget .Button с id 'button1' – user1420185

0

Возможно забыть осуществить OnClickListener?

public class MyActivity extends Activity implements View.OnClickListener 

Или что-то вроде этого:

public class MyActivity extends Activity { 
Button next, previous; 
ViewFlipper vf; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    next = (Button) findViewById(R.id.button1); 
    previous = (Button) findViewById(R.id.button2); 
    next.setOnClickListener(nextOnClick);//<--------new listener 
    previous.setOnClickListener(previousOnClick);//<--------new listener 
    vf = (ViewFlipper) findViewById(R.id.viewFlipper1); 
} 

private View.OnClickListener nextOnClick = new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     vf.showNext(); 
    } 
}; 

private View.OnClickListener previousOnClick = new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     vf.showPrevious(); 
    } 
}; 
+0

private View.OnClickListener nextOnClick = new View.OnClickListener() { @Override public void onClick (Просмотреть просмотр) { vf.showNext(); } } -Eclipse считывает '@Override' как ошибку. – user1420185

+0

Хорошо, я изменил уровень соответствия на 1,6, но я получаю и неожиданную ошибку при нажатии «Следующий» или «Предыдущий» после изменения вида из исходного представления. – user1420185

+0

05-31 04: 35: 32.013: E/AndroidRuntime (424): java.lang.IllegalStateException: Не удалось найти метод button1Click (View) в классе активности first.motivationalquotes.MotivationalQuotesActivity для обработчика onClick в классе вида android.widget .Button с id 'button1' – user1420185